import { ConnectionOptions } from "typeorm";
import { config as configDotenv } from 'dotenv'
import fs from "fs";
import path = require("path");

configDotenv({
  path: path.join(__dirname, '../.env')
});

export type ChainConfig = {
  rpc: string,
  name: string,
  gasPrice: string
};

export type Configuration = {
  chain: ChainConfig,
  tokens: string[],
  ether: string,
  privateKeys: string[],
  server: {
    port: number
  },
  interval: {
    runner: number
  },
  database: ConnectionOptions
};

export const defaultConfig: Configuration = {
  chain: {
    rpc: "https://bsc-dataseed.binance.org",
    name: "bsc",
    gasPrice: "fast"
  },
  ether: "0.01", // BNB 数量
  privateKeys: [], // 私钥
  tokens: [], // Token 白名单
  server: {
    port: 8081
  },
  interval: {
    runner: 2000
  },
  database: {
    type: "mysql",
    host: "47.107.52.171",
    port: 3306,
    username: "spar-test",
    password: "4w48KBJL5cCKNLEt",
    database: "spar-test",
    entityPrefix: "pxtokentransfer_",
    entities: [__dirname + "/entity/*{.ts,.js}"],
    migrations: ["migrations/*.ts"]
  }
}

export let config: Configuration;

export function checkConfig() {
  if (!fs.existsSync("config.json")) {
    fs.writeFileSync("config.json", JSON.stringify(defaultConfig, null, 2));
  }
  config = JSON.parse(fs.readFileSync("config.json").toString());
}
